package cn.hzking.gjmall.app;

import android.app.Activity;
import android.os.Handler;
import android.support.annotation.NonNull;

import java.util.HashMap;

/**
 * Created by yy on 2018/1/24
 */

public final class Configurator {

    public static final HashMap<Object, Object> LATTE_CONFIGS = new HashMap<>();
    private static final Handler HANDLER = new Handler();

    private Configurator() {
        LATTE_CONFIGS.put(ConfigKeys.CONFIG_READY, false);
        LATTE_CONFIGS.put(ConfigKeys.HANDLER, HANDLER);
    }

    static Configurator getInstance() {
        return Holder.INSTANCE;
    }

    final HashMap<Object, Object> getLatteConfigs() {
        return LATTE_CONFIGS;
    }

    private static class Holder {
        private static final Configurator INSTANCE = new Configurator();
    }


    public final void configure() {
        LATTE_CONFIGS.put(ConfigKeys.CONFIG_READY, true);
        GjAppUtils.init(Gjmall.getApplicationContext());

    }
    /**
     *
     * @param appId 正式环境下微信的appid
     * @return
     */
    public final Configurator withWeChatAppId(String appId) {
        LATTE_CONFIGS.put(ConfigKeys.WE_CHAT_APP_ID, appId);
        return this;
    }

    /**
     *
     * @param debugAppId  调试环境的的appid
     * @return
     */
    public final Configurator withDeBugWeChatAppId(String debugAppId) {
        LATTE_CONFIGS.put(ConfigKeys.WE_CHAT_DEBUG_APP_ID, debugAppId);
        return this;
    }

    /**
     *
     * @param apiHost  阿里云正式域名
     * @return
     */
    public final Configurator withOssRealseHost(String apiHost) {
        LATTE_CONFIGS.put(ConfigKeys.OSS_HOST, apiHost);
        return this;
    }

    /**
     *
     * @param debugApiHost 阿里云测试域名
     * @return
     */
    public final Configurator withOssDeBugHost(String debugApiHost) {
        LATTE_CONFIGS.put(ConfigKeys.DEBUG_OSS_HOST, debugApiHost);
        return this;
    }
    /**
     *
     * @param apiHost  阿里云正式域名
     * @return
     */
    public final Configurator withRealseAppHost(String apiHost) {
        LATTE_CONFIGS.put(ConfigKeys.API_HOST, apiHost);
        return this;
    }

    /**
     *
     * @param debugApiHost 阿里云测试域名
     * @return
     */
    public final Configurator withDeBugAppHost(String debugApiHost) {
        LATTE_CONFIGS.put(ConfigKeys.DEBUG_API_HOST, debugApiHost);
        return this;
    }

    /**
     *
     * @param isDeBug 是不是在调试环境
     * @return
     */
    public final Configurator withIsDeBug(boolean isDeBug) {
        LATTE_CONFIGS.put(ConfigKeys.DEBUG_API_HOST, isDeBug);
        return this;
    }
    /**
     *
     * @param appSecret 微信的secret
     * @return
     */
    public final Configurator withWeChatAppSecret(String appSecret) {
        LATTE_CONFIGS.put(ConfigKeys.WE_CHAT_APP_SECRET, appSecret);
        return this;
    }

    public final Configurator withActivity(Activity activity) {
        LATTE_CONFIGS.put(ConfigKeys.ACTIVITY, activity);
        return this;
    }

    /**
     *
     * @param name js与native交互的别名
     * @return
     */
    public Configurator withJavascriptInterface(@NonNull String name) {
        LATTE_CONFIGS.put(ConfigKeys.JAVASCRIPT_INTERFACE, name);
        return this;
    }

    /**
     * 检测是否合法
     */
    private void checkConfiguration() {
        final boolean isReady = (boolean) LATTE_CONFIGS.get(ConfigKeys.CONFIG_READY);
        if (!isReady) {
            throw new RuntimeException("Configuration is not ready,call configure");
        }
    }

    /**
     *
     * @param key 查询的key值
     * @param <T> 返回的对象
     * @return
     */
    @SuppressWarnings("unchecked")
    final <T> T getConfiguration(Object key) {
        checkConfiguration();
        final Object value = LATTE_CONFIGS.get(key);
        if (value == null) {
            throw new NullPointerException(key.toString() + " IS NULL");
        }
        return (T) LATTE_CONFIGS.get(key);
    }
}
